#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;

	RBTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	RBTree() = default;

	RBTree(const RBTree<K, V>& t)
	{
		_root = Copy(t._root);
	}

	RBTree<K, V>& operator=(RBTree<K, V> t)
	{
		swap(_root, t._root);
		return *this;
	}

	~RBTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			//根节点是黑色的
			_root->_col = BLACK;
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		//新增节点颜色给红色
		cur->_col = RED;
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		//存在有parent且parent==RED
		while (parent && parent->_col == RED)
		{
			//情况一：cur为红，parent为红，grandfather为黑
			//解决方式：将p,u改为黑，g改为红，然后把g当成cur，继续向上调整
			Node* grandfather = parent->_parent;
			//      g
			//   p     u 
			if (parent == grandfather->_left)
			{
				//      g
				//   p     u
				// c
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					//uncle存在且为红 -> 变色，再继续往上处理
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//继续向上调整
					//1、parent不存在，cur就是根了，出去后把根处理成黑的
					//2、parent存在且为黑
				   //3、parent存在，且为红，继续循环处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{     //情况二：
					////u存在且为黑 或 不存在 -> 旋转+变色

					//      g
					//   p     u
					// c
					if (cur == parent->_left)//单旋
					{
						RotateR(grandfather);//以g的位置进行右单旋
						//变色
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//      g
						//   p     u
						//     c
						//双旋
						RotateL(parent);
						RotateR(grandfather);

						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					//1、黑色节点的数量没变；2、没有连续的红节点；
					// 3、旋转完之后，p为root且为黑
					break;//直接退出，不用往上处理
				}
			}
			else
			{
				//      g
				//   u     p
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					////uncle存在且为红 -> 变色，再继续往上处理
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//继续往上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					//情况二：
					////u存在且为黑 或 不存在 -> 旋转+变色
					//      g
				   //   u     p
				  //             c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED:
					}
					else
					{
						//      g
					   //   u     p
					  //        c
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
		}
		//此时循环出来，就表示grandfather就为root，root为black

		_root->_col = BLACK;

		return true;
	}


	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	int Hight()
	{
		return _Hight(_root);
	}

	int Size()
	{
		return _Size(_root);
	}





private:
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}

			subR->_parent = parentParent;
		}
	}

	void  RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* parentParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parentParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}
		}
	}

	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;

		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}

	Node* Copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;

		Node* newRoot = new Node(root->_key, root->_value);
		newRoot->_left = Copy(root->_left);
		newRoot->_right = Copy(root->_right);

		return newRoot;
	}

private:
	Node* _root = nullptr;
};

void TestBRTree1()
{
	RBTree<int, int> t;
	int a[] = { 4,2,6,1,3,5,15,7,16,14 };
	for (auto e : a)
	{
		/*if(e==9)
		{
		   int i=0;
		}*/
		t.Insert({ e,e });
		//cout << e << "->" << endl;
	}
	t.InOrder();
}